# -*- coding: utf-8 -*-
'''Copyright 2010 Giordano Tamburrelli. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. Neither the name of Giordano Tamburrelli nor the names of his contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.
   
THIS SOFTWARE IS PROVIDED BY GIORDANO TAMBURRELLI "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL GIORDANO TAMBURRELLI OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.

Created on May 24, 2010

@author: Giordano Tamburrelli
'''
import unittest
import os
from DBLP.Downloader import Downloader
from DBLP.Utils import ends_with
from DBLP.UnicodeTable import replace_unicode
from DBLP.PaperHandlers import SpringerURLHandler, AcmURLHandler, EceasstURLHandler, DoiURLHandler, \
    JucsHandler, DoiComputerHandler, ScienceURLHandler, CrossRefHandler, IeeeURLHandler, InterScienceHandler, DoiIEEEHandler, ComputerHandler
from DBLP.Editors import get_editor

class DBLPTest(unittest.TestCase):
    def setUp(self):
        self.dblp = Downloader(verbose=False)

    def tearDown(self):
        pass
    
class UtilsTest(unittest.TestCase):
    def testUnicdodeReplacement(self):
        res = replace_unicode('Trés bien')
        self.assertEquals(res, 'Tr=eacute=s bien')
        
    def testEndsWith(self):
        test = 'adsfas234asdf2&^%*523 21342123.pdf'
        self.assertTrue(ends_with(test, '.pdf'))
        test = 'adsfas.pdf234523 21342123'
        self.assertFalse(ends_with(test, '.pdf'))
    
class HandlerTest(unittest.TestCase):
    
    def setUp(self):
        from DBLP.Utils import UrlOpener
        self.opener = UrlOpener()
        
    def test_Computer_handler(self):
        url = "http://doi.ieeecomputersociety.org/10.1109/ICPR.2004.50"
        handler = ComputerHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))    
    
    def test_IEEE_handler(self):
        url = "http://dx.doi.org/10.1109/ICSE.2009.5070513"
        handler = DoiURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))    
    
    def test_acm_handler(self):
        url = 'http://doi.acm.org/10.1145/568760.568849'
        handler = AcmURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-")) 

    def test_acm_handler2(self):
        url = 'http://doi.acm.org/10.1145/568760.568849'
        handler = AcmURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))

    def test_portal_acm_handler(self):
        url = 'http://portal.acm.org/citation.cfm?id=302405.302966'
        handler = AcmURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))

    def test_springer_handler(self):   
        url = 'http://link.springer.de/link/service/series/0558/bibs/2505/25050001.htm'
        handler = SpringerURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
        
    def test_eceasst_handler(self):   
        url = 'http://eceasst.cs.tu-berlin.de/index.php/eceasst/article/view/155'
        handler = EceasstURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
        
    def test_doi_handler(self):   
        url = 'http://dx.doi.org/10.1109/ICSE.2009.5070513'
        handler = DoiURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))    
    
    def test_science_handler(self):   
        url = 'http://dx.doi.org/10.1016/j.infsof.2006.07.007'
        handler = ScienceURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
        
    def test_acm_handler3(self):
        url = 'http://doi.acm.org/10.1145/357062.357066' 
        handler = AcmURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-")) 
        
    def test_computer_handler(self):
        url = 'http://csdl.computer.org/comp/proceedings/csmr/2004/2107/00/21070048abs.htm' 
        handler = DoiComputerHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
    
    def test_ieee_handler(self):
        url = 'http://ieeexplore.ieee.org/lpdocs/epic03/wrapper.htm?arnumber=4435102'
        handler = IeeeURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))

    def test_cross_ref_handler(self):
        url = 'http://mr.crossref.org/iPage/?doi=10.1049%2Fiet-sen%3A20070027' 
        handler = CrossRefHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))

    def test_doi_handler_not_found(self):
        url = 'http://dx.doi.org/10.1002/(SICI)1096-908X(199905/06)11:3&lt;201::AID-SMR192&gt;3.0.CO;2-1' 
        handler = DoiURLHandler()
        page = handler.get_paper(url, self.opener)
        self.assertEqual(page, None)

#    def test_doi_ieee_handler(self):
#        #NOT WORKING
#        url = 'http://doi.ieeecomputersociety.org/10.1109/ICWS.2004.23' 
#        handler = DoiIEEEHandler()
#        page = handler.get_paper(url, self.opener)
#        print page
#        self.assertTrue(str(page).startswith("%PDF-"))

    def test_jucs_handler(self):
        url = 'http://www.jucs.org/jucs_9_7/organizing_the_knowledge_used' 
        handler = JucsHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
        
    def test_inter_science_handler(self):   
        url = 'http://dx.doi.org/10.1002/spip.183'
        handler = InterScienceHandler()
        page = handler.get_paper(url, self.opener)
        self.assertTrue(str(page).startswith("%PDF-"))
    
            

class URLTest(DBLPTest):
    
    def __count__(self, prefix, list):
        count = 0
        for item in list:
            if item.startswith(prefix): count+=1
        return count
    
    def test_get_editor(self):
        url = 'http://dx.doi.org/10.1007/978-3-540-73196-2_16'
        editor = get_editor(url)
        self.assertEqual(editor, 'http://dx.doi.org/')
        
    def test_recognize_urls_in_DBLP(self):
        test_file = os.path.join(os.path.dirname(__file__), '..', 'res', 'dblp_test.html')
        openfile = open(test_file, 'r')
        dblp_page = openfile.read()
        res = self.dblp.recognize_urls_in_DBLP(dblp_page)
        found = self.__count__('http://doi.acm.org/', res)
        self.assertEquals(found, 21)
        found = self.__count__('http://doi.ieeecomputersociety.org/', res)
        self.assertEquals(found, 10)
        found = self.__count__('http://dx.doi.org/', res)
        self.assertEquals(found, 32)
        found = self.__count__('http://portal.acm.org/citation.cfm', res)
        self.assertEquals(found, 0)
        found = self.__count__('http://asdfgsdfgs/sadfgsad/', res)
        self.assertEquals(found, 0)
    
    def testSimpleURL(self):
        url = self.dblp.get_DBLP_url('Carlo', 'Ghezzi', None)
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/g/Ghezzi:Carlo.html")
        
    def testLongURL(self):
        url = self.dblp.get_DBLP_url('Grzegorz', 'Rozenberg', None)
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/r/Rozenberg:Grzegorz.html")
    
    def testMultipleMidlleNamesURL(self):
        url = self.dblp.get_DBLP_url('Betty', 'Cheng', ('H.', 'C.',))
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/c/Cheng:Betty_H=_C=.html")
        
    def testHyphenURL(self):
        url = self.dblp.get_DBLP_url('Jean-Marc', 'Thévenin', None)
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/t/Th=eacute=venin:Jean=Marc.html")
        
    def testComplexURL(self):
        url = self.dblp.get_DBLP_url('Linnda', 'Caporeal', ('Caporeal', 'R.'))
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/c/Caporeal:Linnda_Caporeal_R=.html")
        
    def testMiddleNameURL(self):
        url = self.dblp.get_DBLP_url('Miguel', 'Gomes', ('Àngel',))
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/g/Gomes:Miguel_=Agrave=ngel.html")
        
    def testAccentNameURL(self):
        url = self.dblp.get_DBLP_url('Árpád', 'Beszédes', None)
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/b/Besz=eacute=des:=Aacute=rp=aacute=d.html")
        
    def testURLwithInitialAccent(self):
        url = self.dblp.get_DBLP_url('Selma', 'Àbanovic', None)
        self.assertEquals(url, "http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/=/=Agrave=banovic:Selma.html")

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
